package switchviz.graphic.signal;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JColorPanel;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.PaddingPanel;

public class TestSignalPanel extends PaddingPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2829256323272568653L;
	private BasicSignalPersistence _persistenceHandler = null;
	private BasicSignalDataModel _datamodel = null;
	private JLabeldComponent _txtname = null;
	private JColorPanel _clsignal = null;
	private JLabel _lbltype = null;
	private JLabeldComponent _txtminvalue = null;
	private JLabeldComponent _txtmaxvalue = null;
	private JLabeldComponent _chbloweralarm = null;
	private JLabeldComponent _chbupperalarm = null;
	private JLabeldComponent _cmbpriority = null;
	
	public Element getElement()
	{
		getDataModel().setColor(_clsignal.getColor());
		getDataModel().setName(_txtname.getText());
		getDataModel().setMin(Float.parseFloat(_txtminvalue.getText()));
		getDataModel().setMax(Float.parseFloat(_txtmaxvalue.getText()));
		
		int alarmmode = 0;
		if (_chbloweralarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_LOWER_BOUND;
		if (_chbupperalarm.getCheckBox().isSelected())
			alarmmode += BasicSignalDataModel.ALARM_UPPER_BOUND;
		getDataModel().setAlarmmode(alarmmode);
		
		if (_cmbpriority.getText().equals("Simple"))
			getDataModel().setPriority(0);
		else if (_cmbpriority.getText().equals("Detailed"))
			getDataModel().setPriority(1);
		else if (_cmbpriority.getText().equals("Off"))
			getDataModel().setPriority(2);
		
		return _persistenceHandler.setDataModel(getDataModel());
	}
	
	public TestSignalPanel(Element node)
	{
		_persistenceHandler = new BasicSignalPersistence(node);
		initialize();
		this.setDataModel(_persistenceHandler.getDataModelFromElement());
	}
	
	private void initialize()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		_txtname = new JLabeldComponent("Signal Name: ", JLabeldComponent.TEXTFIELD);
		_clsignal = new JColorPanel("Signal Color: ");
		_lbltype = new JLabel();
		_txtminvalue = new JLabeldComponent("Minvalue: ", JLabeldComponent.TEXTFIELD);
		_txtmaxvalue = new JLabeldComponent("Maxvalue: ", JLabeldComponent.TEXTFIELD);
		_chbloweralarm = new JLabeldComponent("Lower Alarm: ", JLabeldComponent.CHECKBOX);
		_chbupperalarm = new JLabeldComponent("Upper Alarm: ", JLabeldComponent.CHECKBOX);
		_cmbpriority = new JLabeldComponent("Priority: ", JLabeldComponent.COMBOBOX);
		_cmbpriority.getComboBox().addItem("Simple");
		_cmbpriority.getComboBox().addItem("Detailed");
		_cmbpriority.getComboBox().addItem("Off");
		
		this.add(_lbltype);
		this.add(_txtname);
		this.add(_cmbpriority);
		this.add(_clsignal);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtminvalue);
		this.add(_txtmaxvalue);
		this.add(_chbloweralarm);
		this.add(_chbupperalarm);
	}
	
	private BasicSignalDataModel getDataModel()
	{
		if(_datamodel == null)
		{
			_datamodel = new BasicSignalDataModel();
			_datamodel.setType(BasicSignalDataModel.TYPETEST);
		}
		return _datamodel;
	}
	
	private void setDataModel(BasicSignalDataModel datamodel)
	{
		_datamodel = datamodel;
		_txtname.setText(_datamodel.getName());
		_clsignal.setColor(_datamodel.getColor());
		_lbltype.setText(_datamodel.getType());
		_txtminvalue.setText(String.valueOf(_datamodel.getMin()));
		_txtmaxvalue.setText(String.valueOf(_datamodel.getMax()));
		
		switch (getDataModel().getAlarmmode())
		{
		case BasicSignalDataModel.ALARM_NO_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_LOWER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(false);
			break;
		case BasicSignalDataModel.ALARM_UPPER_BOUND:
			_chbloweralarm.getCheckBox().setSelected(false);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		case BasicSignalDataModel.ALARM_BOTH_BOUNDS:
			_chbloweralarm.getCheckBox().setSelected(true);
			_chbupperalarm.getCheckBox().setSelected(true);
			break;
		}
		
		switch (getDataModel().getPriority())
		{
		case 0:
			_cmbpriority.setText("Simple");
			break;
		case 1:
			_cmbpriority.setText("Detailed");
			break;
		case 2:
			_cmbpriority.setText("Off");
			break;
		}
	}
}
